package com.sky.seata.order.service.impl;

import com.sky.seata.account.client.IAccountClient;
import com.sky.seata.order.entity.SeataOrder;
import com.sky.seata.order.mapper.SeataOrderMapper;
import com.sky.seata.order.service.ISeataOrderService;
import com.sky.seata.order.util.IdGenerator;
import com.sky.seata.storage.client.IStockClient;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (SeataOrder)表服务实现类
 *
 * @author makejava
 * @since 2020-07-28 17:00:49
 */
@Service("seataOrderService")
public class SeataOrderServiceImpl implements ISeataOrderService {
    @Resource
    private SeataOrderMapper seataOrderMapper;
    @Reference
    private IAccountClient accountClient;
    @Reference
    private IStockClient stockClient;

    /**
     * 新增数据
     *
     * @return 实例对象
     */
    @Override
    @GlobalTransactional
    public SeataOrder insert() {
        SeataOrder order = new SeataOrder();
        order.setOrderId(IdGenerator.GenerateId24());
        order.setOrderName("seata");
        seataOrderMapper.insert(order);
        accountClient.update2();
        return order;
    }

    @Override
    public SeataOrder insert3() {
        for (int i = 0; i < 100; i++) {
            SeataOrder order = new SeataOrder();
            order.setOrderId(IdGenerator.GenerateId24());
            order.setOrderName("seata"+i);
            seataOrderMapper.insert(order);
        }
        return null;
    }

    @Override
    @GlobalTransactional
    public SeataOrder insert2() {
        SeataOrder order = new SeataOrder();
        order.setOrderId(IdGenerator.GenerateId24());
        order.setOrderName("seata");
        seataOrderMapper.insert(order);
        accountClient.update();
        return null;
    }

    @Override
    public SeataOrder selectByMaster() {
        return seataOrderMapper.selectByMaster();
    }

    @Override
    public SeataOrder selectBySlave() {
        return seataOrderMapper.selectBySlave();
    }
}
